using AutoMapper;
using hao_Common.AutoMapper.Profiles;
using hao_Dto;
using hao_Model;
using hao_Model.Enums;
using System;
using System.Linq;

namespace hao_Common.AutoMapper.Profiles
{
    /// <summary>
    /// 商品相关映射配置
    /// </summary>
    public class ProductProfile : BaseProfile
    {
        public ProductProfile()
        {
            // ProductPublishDto 到 Product 的映射
            CreateMap<ProductPublishDto, Product>()
                .ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(src => false))
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id ?? Guid.NewGuid()));

            // Product 到 ProductPublishDto 的映射
            CreateMap<Product, ProductPublishDto>()
                .ForMember(dest => dest.ImageUrls, opt => opt.Ignore());

            // Product 到 ProductResponseDto 的映射
            CreateMap<Product, ProductResponseDto>()
                .ForMember(dest => dest.ImageUrls, opt => opt.Ignore());

            // Product 到 ProductListDto 的映射
            CreateMap<Product, ProductListDto>()
                .ForMember(dest => dest.FirstImageUrl, opt => opt.Ignore())
                .ForMember(dest => dest.DeliveryMethodName, opt => opt.MapFrom(src => src.DeliveryMethod.ToString()))
                .ForMember(dest => dest.BargainOptionName, opt => opt.MapFrom(src => src.BargainOption.ToString()));

            // 图片URL到ProductImage的映射
            CreateMap<string, ProductImage>()
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.ProductId, opt => opt.Ignore())
                .ForMember(dest => dest.Url, opt => opt.MapFrom(src => src))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(src => false));

            // ProductImage 到 string 的映射
            CreateMap<ProductImage, string>()
                .ConvertUsing(src => src.Url);

            // ProductComment 到 ProductCommentDto 的映射
            CreateMap<ProductComment, hao_Dto.Product.ProductCommentDto>()
                .ForMember(dest => dest.UserName, opt => opt.Ignore()) // 由服务层设置
                .ForMember(dest => dest.IsLiked, opt => opt.Ignore()) // 由服务层设置
                .ForMember(dest => dest.Replies, opt => opt.Ignore()); // 由服务层设置
            
            // CreateProductCommentDto 到 ProductComment 的映射
            CreateMap<hao_Dto.Product.CreateProductCommentDto, ProductComment>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => Guid.NewGuid()))
                .ForMember(dest => dest.UserId, opt => opt.Ignore()) // 由服务层设置
                .ForMember(dest => dest.LikeCount, opt => opt.MapFrom(src => 0))
                .ForMember(dest => dest.CreatedAt, opt => opt.MapFrom(src => DateTime.UtcNow))
                .ForMember(dest => dest.UpdatedAt, opt => opt.MapFrom(src => DateTime.UtcNow))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(src => false));

            // ProductCommentLike 相关映射
            CreateMap<ProductCommentLike, object>(); // 基础映射，如果需要DTO可以后续添加
        }
    }
}